unit UFrmFeedReader;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdHTTP, ComCtrls, IdIOHandler, IdIOHandlerStream, OverbyteIcsWndControl,
  OverbyteIcsHttpProt, IdServerIOHandler, IdSSL, IdSSLOpenSSL,
  IdIOHandlerSocket, IdIOHandlerStack;

type
  TForm1 = class(TForm)
    edtURL: TEdit;
    btnStart: TButton;
    edtUser: TEdit;
    edtPassword: TEdit;
    statBar: TStatusBar;
    mmoResult: TMemo;
    btnDownload2: TButton;
    idhttpClient1: TIdHTTP;
    idSSLHandler: TIdSSLIOHandlerSocketOpenSSL;
    procedure btnDownload2Click(Sender: TObject);
    procedure btnStartClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

function Download(URL, User, Pass, FileName: string): Boolean;

implementation

{$R *.dfm}

uses
  WinInet;

function Download(URL, User, Pass, FileName: string): Boolean;
const
  BufferSize = 1024;
var
  hSession, hURL: HInternet;
  Buffer: array[1..BufferSize] of Byte;
  BufferLen: DWORD;
  F: File;
begin
   Result := False;
   hSession := InternetOpen('', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0) ;

   // Establish the secure connection
   InternetConnect (
     hSession,
     PChar(URL),
     INTERNET_DEFAULT_HTTPS_PORT,
     PChar(User),
     PChar(Pass),
     INTERNET_SERVICE_HTTP,
     0,
     0
   );

  try
    hURL := InternetOpenURL(hSession, PChar(URL), nil, 0, 0, 0) ;
    try
      AssignFile(f, FileName);
      Rewrite(f,1);
      try
        repeat
          InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen) ;
          BlockWrite(f, Buffer, BufferLen)
        until BufferLen = 0;
      finally
        CloseFile(f) ;
        Result := True;
      end;
    finally
      InternetCloseHandle(hURL)
    end
  finally
    InternetCloseHandle(hSession)
  end;
end;

procedure TForm1.btnDownload2Click(Sender: TObject);
begin
  Download(edtURL.Text, edtUser.Text, edtPassword.Text, 'c:\temp\zzz.txt');

  mmoResult.Lines.LoadFromFile('c:\temp\zzz.txt');

//  mmoResult.Lines.LoadFromStream(httpcliClient.RcvdStream);
end;

procedure TForm1.btnStartClick(Sender: TObject);
var S: TStringList;
   M: TStream;
   aString : string;
begin
 S := TStringList.Create;
 M := TMemoryStream.Create;
 try
//   S.Values['Email'] := edtUser.Text;
//   S.Values['Passwd'] := edtPassword.Text;
//   S.Values['source'] := 'estream-sqloffice-1.1.1.1';
//   S.Values['service'] := 'cl';

   idhttpClient1.HandleRedirects := True;
   idhttpClient1.IOHandler := idSSLHandler;
   idhttpClient1.Request.Username := edtUser.Text;
   idhttpClient1.Request.Password := edtPassword.Text;

   aString := idhttpClient1.Get(edtURL.Text);
   mmoResult.Lines.Text:= aString;

//   idhttpClient1.Request.ContentType := 'application/x-www-form-urlencoded';
//   idhttpClient1.Post('https://www.google.com/accounts/ClientLogin', S, M);
//   mmoResult.Lines.Add(Format('Response Code: %d', [idhttpClient1.ResponseCode]));
//   mmoResult.Lines.Add(Format('Response Text: %s', [idhttpClient1.ResponseText]));

   M.Position := 0;
   S.LoadFromStream(M);
   mmoResult.Lines.AddStrings(S);
 finally
   S.Free;
   M.Free;
 end;
end;
end.
